import itertools
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import rcParams

def plot_confusion_matrix(cm, classes, normalize=False,  cmap=plt.cm.Blues):
    plt.figure()
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    # plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    plt.axis("equal")

    ax = plt.gca()
    left, right = plt.xlim()
    ax.spines['left'].set_position(('data', left))
    ax.spines['right'].set_position(('data', right))
    for edge_i in ['top', 'bottom', 'right', 'left']:
        ax.spines[edge_i].set_edgecolor("white")

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        num = '{:.2f}'.format(cm[i, j]) if normalize else int(cm[i, j])
        plt.text(j, i, num,
                 verticalalignment='center',
                 horizontalalignment="center",
                 color="white" if num > thresh else "black")

    plt.ylabel('Actual label')
    # plt.ylabel('Self patt')
    plt.xlabel('Predict label')
    plt.xticks(rotation=0)
    # plt.xlabel('Transition patt')
    plt.tight_layout()
    plt.savefig('method_2.png', transparent=True, dpi=800)

    plt.show()


trans_mat = np.array([[68504, 93],
                      [369, 43034]], dtype=int)

"""method 2"""
if __name__ == '__main__':

    

    config = {
        "font.family": 'serif', # 衬线字体
        "font.size": 10, # 相当于小四大小
        "font.serif": ['SimSun'], # 宋体
        "mathtext.fontset": 'stix', # matplotlib渲染数学字体时使用的字体，和Times New Roman差别不大
        'axes.unicode_minus': False # 处理负号，即-号
    }
    rcParams.update(config)

    # label = ["Patt {}".format(i) for i in range(1, trans_mat.shape[0] + 1)]
    label = ['normal','abnormal']
    plot_confusion_matrix(trans_mat, label)